<template>
  <a-modal
    class="profile-task-dialog formModal editor"
    v-model:open="dialogVisible"
    width="37.5vw"
    :footer="null"
    :title="$t('dialog.task_follow.title')"
  >
    <div>
      <div class="task-detail" v-if="data">
        <header>
          <div class="title">
            <span class="bold">{{ data.title }}</span>
            <span
              >（{{ $t("dialog.task_detail.task_no") }}：{{
                data.task_no
              }}）</span
            >
          </div>
          <div class="footer">
            <div class="publisher">
              <IconUser></IconUser>
              <span
                >{{ $t("dialog.task_detail.publisher") }}：{{
                  data.member_last_name +' ' + data.member_first_name
                }}</span
              >
            </div>
            <div class="create_time">
              {{ $t("dialog.task_detail.create_time") }}：{{ data.create_time }}
            </div>
          </div>
        </header>
      </div>

      <div class="follow-records">
        <template v-if="data && followData">
          <div class="records-list">
            <div
              class="record-item"
              v-for="(recordItem, recordItemIndex) in followData"
              :key="recordItem.id"
            >
              <div class="title">
                <p>
                  {{ $t("dialog.profile_task.task_progress") }}：{{
                    recordItem.create_time
                  }}
                </p>
                <div>
                  <div class="reciever-info">
                    <span class="label"
                      >{{
                        recordItem.member_id == data.member_id
                          ? $t("table.profile_task.col_2")
                          : $t("content.task.receiver")
                      }}：</span
                    >
                    <span class="user reciever">{{
                      recordItem.member_last_name + ' ' + recordItem.member_first_name
                    }}</span>
                  </div>
                  <span class="replay-label"
                    >{{ $t("content.task.replay") }}：</span
                  >
                </div>
              </div>
              <div class="record-card raw-html">
                <div class="ql-editor" v-html="recordItem.content"></div>
              </div>
              <template
                v-if="data.applicant_status == 0 || data.applicant_status == 3"
              >
                <div
                  class="btns"
                  v-if="recordItemIndex == followData.length - 1"
                >
                  <a-button
                    type="primary"
                    danger
                    size="small"
                    ghost
                    @click="continueFollowUp"
                    v-if="
                      data.status != 4 &&
                      data.receive_id == memberStore.profile?.id
                    "
                    >{{ $t("btnText.continue_follow_task") }}</a-button
                  >
                  <a-button
                    type="primary"
                    size="small"
                    v-if="
                      data.status != 4 &&
                      data.receive_id == memberStore.profile?.id
                    "
                    @click="applyToStatement"
                    >{{ $t("btnText.apply_statements") }}</a-button
                  >
                  <a-button
                    type="primary"
                    size="small"
                    v-if="
                      data.status != 4 &&
                      data.member_id == memberStore.profile?.id
                    "
                    @click="replayRecord"
                    >{{ $t("btnText.replay") }}</a-button
                  >
                  <a-button
                    type="primary"
                    size="small"
                    v-if="
                      data.status != 4 &&
                      data.member_id == memberStore.profile?.id
                    "
                    @click="applyToStatement"
                    >{{ $t("btnText.to_statements") }}</a-button
                  >
                </div>
              </template>
            </div>
          </div>
        </template>
        <template v-else> </template>
      </div>
    </div>
  </a-modal>
  <!-- 任务填写表单 -->
  <DialogTaskFill
    ref="dialogTaskFillRef"
    :type="dialogTaskFillType"
    :id="id"
    :task_title="task_title"
    @complete="getFollowData"
  />
</template>

<script setup lang="ts">
import { computed, getCurrentInstance, nextTick, ref } from "vue";
import {
  postTaskDetail,
  postTaskFollowUpList,
  postTaskFollowUpToStatement,
} from "../../../api/task";
import { useMemberStore } from "../../../store";
import { message, Modal } from "ant-design-vue";

const $t = getCurrentInstance()?.appContext.config.globalProperties.$t;
const memberStore = useMemberStore();

type Props = {
  id: any;
  task_title?: string;
};
const emits = defineEmits(["apply-statement"]);

const dialogVisible = ref(false);

const props = defineProps<Props>();

const data = ref();
const followData = ref();
let cache_id: any = null;
const showDialog = async () => {
  dialogVisible.value = true;
  if (cache_id === props.id) {
    getFollowData();
    return;
  }
  data.value = null;
  followData.value = null;
  postTaskDetail(props.id).then((res) => {
    data.value = res.data;
  });
  getFollowData();
  cache_id = props.id;
};

const getFollowData = () => {
  postTaskFollowUpList(props.id).then((res) => {
    followData.value = res.data;
  });
};

const continueFollowUp = () => {
  dialogTaskFillType.value = "follow";
  nextTick(() => {
    dialogTaskFillRef.value.showDialog();
  });
};

// 回复按钮
const replayRecord = () => {
  dialogTaskFillType.value = "replay";
  nextTick(() => {
    dialogTaskFillRef.value.showDialog();
  });
};

// 申请结单
const applyStatementing = ref(false);
const applyToStatement = () => {
  let confirmModal = Modal.confirm({
    title: $t("dialog.task.ensure_apply_statement.title"),
    content: $t("dialog.task.ensure_apply_statement.content"),
    icon: () => h("", ""),
    async onOk() {
      applyStatementing.value = true;
      confirmModal.update({
        okButtonProps: {
          loading: applyStatementing.value,
        },
      });
      let res = await postTaskFollowUpToStatement(props.id);
      if (res.code == 200) {
        message.success($t("message.success.task.apply_statement"));
        emits("apply-statement");
      } else {
        message.error($t("message.error.task.apply_statement"));
      }
      cache_id = null;
      dialogVisible.value = false;

      confirmModal.update({
        okButtonProps: {
          loading: applyStatementing.value,
        },
      });
    },
    onCancel() {
    },
    class: "profile-confirm-modal",
    okText: $t("btnText.ensure"),
    cancelText: $t("btnText.cancel1"),
    width: "19.7917vw",
    maskClosable: true,
    centered: true,
  });
};

// dialogTaskFill =====================================
const dialogTaskFillRef = ref();
const dialogTaskFillType = ref();

defineExpose({
  showDialog,
});
</script>
<style lang="less">
</style>

<style scoped lang="less">
.task-detail {
  header {
    margin-left: -24px;
    margin-right: -24px;
    background-color: #f6f8fa;
    padding: 24px;
    color: rgb(64, 73, 96);
    font-size: 14px;
    .bold {
      font-size: 18px;
      font-weight: 600;
    }
    .footer {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 16px;
      & > * {
        display: flex;
        align-items: center;
        gap: 8px;
      }
    }
  }
}

.follow-records {
  margin-top: 30px;
  font-size: 16px;
  .records-list {
    display: flex;
    flex-direction: column;
    gap: 13px;
  }
}
.record-item {
  .title {
    margin-bottom: 13px;
    & > div {
      display: flex;
      flex-wrap: wrap;
      column-gap: 12px;
    }
    .user {
      &.reciever {
        color: #1441aa;
      }
      &.publisher {
        color: #f00;
      }
    }
  }
  .record-card {
    background-color: #faf9f9;
    padding: 20px;
    border-radius: 6px;
    .more-detail {
      margin-top: 18px;
      .card-imgs {
        margin-top: 18px;
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        div {
          position: relative;
          &::before {
            inset: 0;
            content: "";
            position: absolute;
            background-color: rgba(0, 0, 0, 0.1);
            z-index: 1;
          }
          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
        }
      }
    }
    .card-files {
      margin-top: 18px;
    }
    .card-files,
    .card-links {
      a {
        color: #1441aa !important;
      }
    }
    .card-links {
      margin-top: 18px;
      display: flex;
    }
  }
  .btns {
    display: flex;
    justify-content: flex-end;
    margin-top: 10px;
    gap: 11px;
    .ant-btn {
      height: 40px !important;
      &.ant-btn-dangerous {
        color: #ff4d4f !important;
      }
      &.ant-btn-primary:not(.ant-btn-dangerous) {
        background-color: #1441aa !important;
      }
    }
  }
}
</style>
